#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef pair<int, int> PII; // <数值和封闭数量>

int n;
int a[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1};
vector<PII> v; 

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		int b;	cin >> b;
		int c = b, num = 0;
		while (c) 
		{
			num += a[c % 10];
			c /= 10;
		}
		v.push_back({num, b});
	}

	sort(v.begin(), v.end());

	for (int i = 0; i < n; i++)
		cout << v[i].second << " ";
	cout << endl;

	return 0;
}